<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    // 1.数组解构赋值
    let arr = [1, [2, 3], 4]
    let [a, [b], d, f = '默认值'] = arr;
    console.log(a);  // 1
    console.log(b);  // 2 
    console.log(d);  // 4
    console.log(f);  // 还可以有默认值 如果不设置默认值 且原数组找不到该项的话 返回undefined


    // 2.对象的解构
    // let obj = {
    //   name: "张三",
    //   age: 20,
    //   height: "178cm"
    // }

    // 直接 把对象的键名和键值设置成一样，简写一个 ；
    // let { name, age, height } = obj;
    // console.log(name, age, height);


    // ES6对象里 如果键名和键值一样 那么可以省略一个 ；

    // let name = "张三";
    // let age = 30;
    // let obj = {
    //     name,
    //     age
    // }
    // console.log(obj)
    let obj = {
      name: "张小三",
      hobby: ["张三", "李四", "王五"],
      obj1: {
        myname: "王健林",
        age: 60
      }
    }
    let { name, hobby, obj1 } = obj
    console.log(name, hobby[1], obj1.age);


    // 2. 解构函数
    const firstName = 'Stephen';
    const lastName = 'Curry';
    const person = {
      firstName,
      lastName,
    }
    console.log(person.firstName + ' ' + person.lastName); // Stephen Curry

    function sayName (person) {
      if (Object.prototype.toString.call(person) == '[object Object]') {
        console.log(`${person.firstName} ${person.lastName}`);
      }
    }
    sayName({
      firstName: 'Stephen',
      lastName: 'Curry'
    });



  </script>
</body>

</html>